package com.tobby.camel.camelinaction.chapter2.filter;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

public class OrderRouterWithFilter {

	public static void main(String[] args) throws Exception {
		CamelContext context = new DefaultCamelContext();
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				"vm://localhost");
		context.addComponent("jms",
				JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
		context.addRoutes(new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from("file:src/main/resources/data?noop=true").to("jms:incomingOrders");
				from("jms:incomingOrders").choice()
						.when(header("CamelFileName").endsWith(".xml"))
						.to("jms:xmlOrder");
				from("jms:xmlOrder").filter(xpath("/order[not(@test)]"))
						.process(new Processor() {

							@Override
							public void process(Exchange exchange) throws Exception {
								System.out.println("Received XML Order : "
										+ exchange.getIn().getHeader("CamelFileName"));
							}
						});
			}
		});
		context.start();
		Thread.sleep(10000);
		context.stop();
	}

}
